package dao

import (
	"gostore/model"
)

type UserDaoT struct {
	DaoT
}

var UserDao = UserDaoT{}

type ResUser struct {
	Uid         int     `json:"uid"`         //用户id
	Account     string  `json:"accout"`      //用户账号
	RealName    string  `json:"realName"`    //真实姓名
	Birthday    int     `json:"birthday"`    //生日
	CardId      string  `json:"cardId"`      //身份证号码
	Mark        string  `json:"mark"`        //用户备注
	GroupId     int     `json:"groupId"`     //用户分组id
	PartnerId   int     `json:"partnerId"`   //合伙人id
	Addres      string  `json:"addres"`      //详细地址
	Nickname    string  `json:"nickName"`    //用户昵称
	Avatar      string  `json:"avatar"`      //用户头像
	Phone       string  `json:"phone"`       //手机号码
	AddTime     int     `json:"addTime"`     //添加时间
	AddIp       string  `json:"addIp"`       //添加ip
	LastTime    int     `json:"lastTime"`    //最后一次登录时间
	LastIp      string  `json:"lastIp"`      //最后一次登录ip
	NowMoney    float64 `json:"nowMoney"`    //用户余额
	Integral    float64 `json:"integral"`    //用户剩余积分
	SignNum     int     `json:"signNum"`     //连续签到天数
	Status      int     `json:"status"`      //1为正常，0为禁止
	Level       int     `json:"level"`       //等级
	SpreadUid   int     `json:"spreadUid"`   //推广元id
	SpreadTime  int     `json:"spreadTime"`  //推广员关联时间
	SpreadName  string  `json:"spreadName"`  // 推广人昵称
	UserType    string  `json:"userType"`    //用户类型
	IsPromoter  int     `json:"isPromoter"`  //是否为推广员
	PayCount    int     `json:"payCount"`    //用户购买次数
	SpreadCount int     `json:"spreadCount"` //下级人数
	CleanTime   int     `json:"cleanTime"`   //等级清除时间为0没有清除过
}

type ReqUserQuery struct {
	Query
	RealNameOrId string `form:"realNameOrId" json:"realNameOrId"` // 姓名编号
	UserType     string `form:"usertype" json:"usertype"`         // 用户类型
	Status       int    `form:"status" json:"status"`             // 状态
	Sex          int    `form:"sex" json:"sex"`                   // 性别
	UserIdentidy int    `form:"userIdentidy" json:"userIdentidy"` // 身份
	UserTime     string `form:"userTime" json:"userTime"`         // 选择时间
	PayCount     string `form:"payCount" json:"payCount"`         // 消费情况
	Level        int    `form:"level" json:"level"`               // 会员等级
}

func (udao UserDaoT) UserList(req *ReqUserQuery) (r []*ResUser, count int64, err error) {
	db := GetDB()
	if req.RealNameOrId != "" {
		db = db.Where("real_name = ?", req.RealNameOrId)
	}
	if req.UserType != "" {
		db = db.Where("user_type = ? ", req.UserType)
	}
	if req.Status >= 0 {
		db = db.Where("status = ?", req.Status)
	}
	if req.UserIdentidy >= 0 {
		db = db.Where("is_promoter = ?", req.UserIdentidy)
	}
	if req.Level >= 0 {
		db = db.Where("level = ?", req.Level)
	}

	limit := 10
	page := 0

	if req.Page > 0 {
		page = req.Page - 1
	}
	if req.PageNum > 0 {
		limit = req.PageNum
	}

	offset := limit * page

	userList := make([]*model.User, 0)

	err = db.Offset(offset).Limit(limit).Find(&userList).Count(&count).Error
	if err != nil {
		return
	}

	r = make([]*ResUser, 0)
	spreadUids := make([]int, 0)
	for _, item := range userList {
		r = append(r, &ResUser{
			Uid:         item.Uid,
			Account:     item.Account,
			RealName:    item.RealName,
			Birthday:    item.Birthday,
			CardId:      item.CardId,
			Mark:        item.Mark,
			GroupId:     item.GroupId,
			PartnerId:   item.PartnerId,
			Addres:      item.Addres,
			Nickname:    item.Nickname,
			Avatar:      item.Avatar,
			Phone:       item.Phone,
			AddTime:     item.AddTime,
			AddIp:       item.AddIp,
			LastTime:    item.LastTime,
			LastIp:      item.LastIp,
			NowMoney:    item.NowMoney,
			Integral:    item.Integral,
			SignNum:     item.SignNum,
			Status:      item.Status,
			Level:       item.Level,
			SpreadUid:   item.SpreadUid,
			SpreadTime:  item.SpreadTime,
			UserType:    item.UserType,
			IsPromoter:  item.IsPromoter,
			PayCount:    item.PayCount,
			SpreadCount: item.SpreadCount,
			CleanTime:   item.CleanTime,
		})
		if item.SpreadUid != 0 {
			spreadUids = append(spreadUids, item.SpreadUid)
		}

	}

	if len(spreadUids) <= 0 {
		return
	}

	spreadUsers := make([]*model.User, 0)

	err = db.Where("uid in(?)", spreadUids).Find(&spreadUsers).Error
	if err != nil {
		return
	}

	mUsers := make(map[int]*model.User, 0)
	for _, item := range spreadUsers {
		mUsers[item.Uid] = item
	}

	for _, item := range r {
		if v, ok := mUsers[item.Uid]; ok {
			item.SpreadName = v.RealName
		}
	}

	return
}
